package com.bizprocess;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class TTest {
	@org.junit.Test
	public void test() {
		ProcessEngine engine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"temp/activiti.cfg.xml").buildProcessEngine();
		RepositoryService repositoryService = engine.getRepositoryService();
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("diagrams/ActivityQuery.bpmn").deploy();
		TaskService taskService = engine.getTaskService();
		RuntimeService runtimeService = engine.getRuntimeService();
		HistoryService historyService=engine.getHistoryService();
		// 开始两条流程
		ProcessInstance pi1 = runtimeService
				.startProcessInstanceByKey("testProcess");
		ProcessInstance pi2 = runtimeService
				.startProcessInstanceByKey("testProcess");
		// 结束第一个流程实例
		Execution exe = runtimeService.createExecutionQuery()
				.processInstanceId(pi1.getId()).singleResult();
//		runtimeService.signal(exe.getId());
		Task task1=taskService.createTaskQuery().processInstanceId(pi1.getId()).singleResult();
		taskService.complete(task1.getId());
		runtimeService.signalEventReceived("mySignal");
		// 第二个流程实例完成第一个任务
		Task task = taskService.createTaskQuery()
				.processInstanceId(pi2.getId()).singleResult();
		taskService.complete(task.getId());
		// 查询数据
		List<HistoricActivityInstance> datas = historyService
				.createHistoricActivityInstanceQuery().activityId("endevent1")
				.list();
		System.out.println("使用activityId查询：" + datas.size());// 结果1
		datas = historyService.createHistoricActivityInstanceQuery()
				.activityInstanceId(datas.get(0).getId()).list();
		System.out.println("使用activityInstanceId查询：" + datas.size());// 结果1
		datas = historyService.createHistoricActivityInstanceQuery()
				.activityType("intermediateSignalCatch").list();
		System.out.println("使用activityType查询：" + datas.size());// 结果2
		datas = historyService.createHistoricActivityInstanceQuery()
				.executionId(exe.getId()).list();
		System.out.println("使用executionId查询：" + datas.size());// 结果3
		datas = historyService.createHistoricActivityInstanceQuery().finished()
				.list();
		System.out.println("使用finished查询：" + datas.size());// 结果6
		datas = historyService.createHistoricActivityInstanceQuery()
				.processInstanceId(pi2.getId()).list();
		System.out.println("使用processInstanceId查询：" + datas.size());// 结果3
		datas = historyService.createHistoricActivityInstanceQuery()
				.taskAssignee("crazyit").list();
		System.out.println("使用taskAssignee查询：" + datas.size());// 结果2
		datas = historyService.createHistoricActivityInstanceQuery()
				.unfinished().list();
		System.out.println("使用unfinished查询：" + datas.size());// 结果1
	}

}
